/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package apptodate;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;

/**
 *@author Revamp Group
 * Bacalanmo, Yra
 * Damot, Peter Paul
 * Delos Santos, Denmark
 * Flores, Jann Marie
 * Ingco, Chester
 * Lao, Charles
 * Ligos, Klarence
 * Navarro, Frances Joffany
 * Pamintuan, Louisa
 * Sanchez, Ashley
 * Toledo, Jazelle Mae
 * Venturina, Kevin 
 */

public class viewAll extends javax.swing.JFrame {

    /**
     * Creates new form viewAll
     */
    private String stringPath;
    private String[] data;
    private String[][] info;
    private String[][] info2;
    
    public viewAll() {
        initComponents();
    }
    

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        settings1 = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jEditorPane1 = new javax.swing.JEditorPane();
        jPanel1 = new javax.swing.JPanel();
        modifyAdd = new javax.swing.JButton();
        settings = new javax.swing.JButton();
        sortByBox = new javax.swing.JComboBox();
        sortByLabel = new javax.swing.JLabel();
        updateButton = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        eventsTable = new javax.swing.JTable();

        settings1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/apptodate/images/wrench.png"))); // NOI18N
        settings1.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        settings1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                settings1ActionPerformed(evt);
            }
        });

        jScrollPane2.setViewportView(jEditorPane1);

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        jPanel1.setBackground(new java.awt.Color(34, 34, 34));

        modifyAdd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/apptodate/images/add.png"))); // NOI18N
        modifyAdd.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        modifyAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                modifyAddActionPerformed(evt);
            }
        });

        settings.setIcon(new javax.swing.ImageIcon(getClass().getResource("/apptodate/images/wrench.png"))); // NOI18N
        settings.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        settings.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                settingsActionPerformed(evt);
            }
        });

        sortByBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Day", "Month", "Year" }));
        sortByBox.setToolTipText("");

        sortByLabel.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        sortByLabel.setForeground(new java.awt.Color(240, 240, 240));
        sortByLabel.setText("Sort By:");

        updateButton.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        updateButton.setText("UPDATE");
        updateButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                updateButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(updateButton)
                .addGap(18, 18, 18)
                .addComponent(sortByLabel)
                .addGap(16, 16, 16)
                .addComponent(sortByBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(modifyAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(settings, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(settings, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(modifyAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(0, 0, Short.MAX_VALUE))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(16, 16, 16)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(sortByBox)
                    .addComponent(updateButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(sortByLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        if(checkPink() == 1){
            modifyAdd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/apptodate/images/pinkAdd.png"))); // NOI18N
        }
        if(checkPink() == 1){
            settings.setIcon(new javax.swing.ImageIcon(getClass().getResource("/apptodate/images/pinkWrench.png"))); // NOI18N
        }

        jPanel2.setBackground(new java.awt.Color(51, 61, 68));
        jPanel2.setPreferredSize(new java.awt.Dimension(5, 351));

        eventsTable.setBackground(new java.awt.Color(51, 61, 68));
        eventsTable.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        eventsTable.setForeground(new java.awt.Color(240, 240, 240));
        eventsTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Date Start", "Event", "DeadLine", "End Time"
            }
        ));
        eventsTable.setPreferredSize(new java.awt.Dimension(561, 440));
        jScrollPane1.setViewportView(eventsTable);
        if(checkPink() == 1){
            eventsTable.setBackground(new java.awt.Color(255, 174, 201));
            eventsTable.setForeground(new java.awt.Color(0,0,0));
        }else{
            eventsTable.setBackground(new java.awt.Color(51, 61, 68));
            eventsTable.setForeground(new java.awt.Color(240,240,240));
        }

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 545, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 351, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, 545, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        if(checkPink() == 1){
            jPanel1.setBackground(new java.awt.Color(255, 9, 88));
        }else{
            jPanel1.setBackground(new java.awt.Color(34, 34, 34));
        }

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void modifyAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_modifyAddActionPerformed
        // TODO add your handling code here:
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ModifyAdd().setVisible(true);
            }
        });
    }//GEN-LAST:event_modifyAddActionPerformed

    private int checkPink(){
        Path path = Paths.get("");
        Path realPath;
        int check = 0;
        try {
            realPath = path.toRealPath(LinkOption.NOFOLLOW_LINKS);
            stringPath = realPath.toString();
            stringPath = stringPath.replace("\\","\\\\");
        } catch (IOException ex) {
            Logger.getLogger(Settings.class.getName()).log(Level.SEVERE, null, ex);
        }
        File pink = new File(stringPath + "\\AppToDate\\pink.txt");
        if(pink.exists()){
            check = 1;
        }
        return check;
    }
    
    private void settingsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_settingsActionPerformed
        // TODO add your handling code here:
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Settings().setVisible(true);
            }
        });
    }//GEN-LAST:event_settingsActionPerformed

    private void settings1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_settings1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_settings1ActionPerformed

    private void updateButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_updateButtonActionPerformed
        try {
            // TODO add your handling code here:
        
        fileInfo();
        DefaultTableModel model = (DefaultTableModel) eventsTable.getModel();
        model.setRowCount(0);
       
        sort();
        for(int count = 0; count < countFiles(); count++){
        model.addRow(new Object[]{info2[count][0], info2[count][1], info2[count][2], info2[count][3]});
        }
            } catch (IOException ex) {
            Logger.getLogger(viewAll.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }//GEN-LAST:event_updateButtonActionPerformed
    
   
    
    private void sort(){
        int arr[];
        int sortBy = 0;
        if(sortByBox.getSelectedItem().equals("Day")){
           sortBy = 2;
        }
        if(sortByBox.getSelectedItem().equals("Month")){
           sortBy = 1;
        }
        if(sortByBox.getSelectedItem().equals("Year")){
           sortBy = 3;
        }
        int sort[][] = new int[countFiles()][3];
        for(int counter = 0; counter < countFiles(); counter++){
            String parts[];
            parts = info2[counter][2].split(" ");
            parts[1] = parts[1].trim();
            parts[2] = parts[2].replace(",", "");
    
            if(parts[1].equals("JAN")){
               parts[1] = "1"; 
            }
            if(parts[1].equals("FEB")){
               parts[1] = "2"; 
            }
            if(parts[1].equals("MAR")){
               parts[1] = "3"; 
            }
            if(parts[1].equals("APR")){
               parts[1] = "4"; 
            }
            if(parts[1].equals("MAY")){
               parts[1] = "5"; 
            }
            if(parts[1].equals("JUN")){
               parts[1] = "6"; 
            }
            if(parts[1].equals("JUL")){
               parts[1] = "7"; 
            }
            if(parts[1].equals("AUG")){
               parts[1] = "8"; 
            }
            if(parts[1].equals("SEP")){
               parts[1] = "9"; 
            }
            if(parts[1].equals("OCT")){
               parts[1] = "10"; 
            }
            if(parts[1].equals("NOV")){
               parts[1] = "11"; 
            }
            if(parts[1].equals("DEC")){
               parts[1] = "12"; 
            }
            sort[counter][0] = Integer.parseInt(parts[1]); //MONTH 1
            sort[counter][1] = Integer.parseInt(parts[2]); //DAY 2
            sort[counter][2] = Integer.parseInt(parts[3]); //YEAR 3
        }
        
        int temp;
        String temp1, temp2, temp3, temp4;
        for (int i = 1; i < sort.length; i++) {
            for(int j = i ; j > 0 ; j--){
                if(sort[j][sortBy - 1] < sort[j-1][sortBy - 1]){
                    temp = sort[j][sortBy - 1];
                    sort[j][sortBy - 1] = sort[j-1][sortBy - 1];
                    sort[j-1][sortBy - 1] = temp;
                    
                    temp1 = info2[j][0];
                    info2[j][0] = info2[j-1][0];
                    info2[j-1][0] = temp1;
                    
                    temp2 = info2[j][1];
                    info2[j][1] = info2[j-1][1];
                    info2[j-1][1] = temp2;
                    
                    temp3 = info2[j][2];
                    info2[j][2] = info2[j-1][2];
                    info2[j-1][2] = temp3;
                    
                    temp4 = info2[j][3];
                    info2[j][3] = info2[j-1][3];
                    info2[j-1][3] = temp4;
                }
            }
        }
    }
    
    
    private void fileInfo() throws FileNotFoundException, IOException{
        int fileCount = countFiles();
        int counter = 0;
        data = new String[fileCount];
        File folder = new File(getPath() + "\\AppToDate\\Events");
        File[] listOfFiles = folder.listFiles();
        if(folder.exists()){
        for (int i = 0; i < listOfFiles.length; i++) {
                data[counter] = listOfFiles[i].getName();
                counter++;
            
        }
        }
        info = new String[fileCount][4];
        info2 = new String[fileCount][4];
        for(int num = 0; num < fileCount; num++){
        String[] part = data[num].split("-");
        
        info[num][0] = part[0];
        info[num][1] = part[1];
        info[num][2] = part[2];        
        info[num][3] = part[3];
        
        }
        
        
        
        for(int num = 0; num < fileCount; num++){
        BufferedReader br = new BufferedReader(new FileReader(getPath() + "\\AppToDate\\Events\\" + data[num]));
        String line = null;
        while ((line = br.readLine()) != null) {
        String[] part = line.split("\\+");
        info2[num][0] = part[0];
        info2[num][1] = part[1];
        info2[num][2] = part[2];
        info2[num][3] = part[3];        
            }
 }
    }
    
    public String getPath(){
        Path path = Paths.get("");
        Path realPath;
        try {
            realPath = path.toRealPath(LinkOption.NOFOLLOW_LINKS);
            stringPath = realPath.toString();
            stringPath = stringPath.replace("\\","\\\\");
        } catch (IOException ex) {
            Logger.getLogger(Settings.class.getName()).log(Level.SEVERE, null, ex);
        }
        String myPathData = "";
        File file2 = new File(stringPath + "\\AppToDate\\configuration.txt");
        String myConfig = file2.toString();
        try {
            FileReader fr = new FileReader(myConfig);
            BufferedReader textRead = new BufferedReader(fr);
            
            try {
                myPathData = textRead.readLine();
            } catch (IOException ex) {
                Logger.getLogger(ModifyAdd.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(ModifyAdd.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return myPathData;
    }
    private int countFiles(){
        File f = new File(getPath() + "\\AppToDate\\Events");
        int count = 0;
        if(f.exists()){
            for (File file : f.listFiles()) {
                if (file.isFile()) {
                    count++;
                    }
                }
        }
        return count;
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(viewAll.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(viewAll.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(viewAll.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(viewAll.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new viewAll().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTable eventsTable;
    private javax.swing.JEditorPane jEditorPane1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JButton modifyAdd;
    private javax.swing.JButton settings;
    private javax.swing.JButton settings1;
    private javax.swing.JComboBox sortByBox;
    private javax.swing.JLabel sortByLabel;
    private javax.swing.JButton updateButton;
    // End of variables declaration//GEN-END:variables
}
